package com.example.treehole.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.treehole.R;
import com.example.treehole.api.Api;
import com.example.treehole.api.ApiConfig;
import com.example.treehole.api.Callback;
import com.example.treehole.entity.LoginResponse;
import com.example.treehole.utils.CountDownTimerUtils;
import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;


public class RegisterActivity extends BaseActivity {

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.et_password)
    EditText etPassword;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.et_code)
    EditText etCode;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.et_account)
    EditText etAccount;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    private String account, password, code;
    private String serverCode;
    private LoadingDialog loadingDialog;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {
        //StatusBarUtils.setStatusBarMode(this,true,R.color.white);
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                //开始倒计时
                CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCode, 60000, 1000);
                countDownTimerUtils.start();
            }else if (msg.what==2){
                loadingDialog.close();
            }
        }
    };

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.iv_exit, R.id.tv_get_code, R.id.btn_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_exit:
                finish();
                break;
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.btn_register:
                if (isValid())
                    register();
                break;
        }
    }

    private boolean isValid() {
        account = etAccount.getText().toString();
        password = etPassword.getText().toString();
        code = etCode.getText().toString();
        if (TextUtils.isEmpty(account)) {
            showToast(getString(R.string.input_account));
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            showToast(getString(R.string.input_password));
            return false;
        }
        if (TextUtils.isEmpty(code)) {
            showToast(getString(R.string.input_code));
            return false;
        }
        return true;
    }


    private void register() {
        HashMap<String, String> mParams = new HashMap<>();
        mParams.put("username", account);
        mParams.put("password", password);
        mParams.put("code", code);
        loadingDialog=new LoadingDialog(this);
        loadingDialog.setLoadingText("验证中").show();
        Api.config(ApiConfig.REGISTER_URL, mParams).postRequest(this, new Callback() {
            @Override
            public void onSuccess(String res) {
                LoginResponse loginResponse = new Gson().fromJson(res, LoginResponse.class);
                if (loginResponse.getCode().equals("200")){
                    //保存一些信息
                    saveStringToSp("token", loginResponse.getToken());
                    saveStringToSp("account", account);
                    saveStringToSp("id", loginResponse.getId());
                    mHandler.sendEmptyMessage(2);
                    jumpTo(MainActivity.class);
                    showToastSync(getString(R.string.login_success));
                    finish();
                }
                else{
                    mHandler.sendEmptyMessage(2);
                    showToastSync(loginResponse.getMsg());
                }
            }
            @Override
            public void onFailure(Exception e) {
                showToastSync(getString(R.string.check_network));
            }
        });
    }

    private void getCode() {
        String account = etAccount.getText().toString();
        if (TextUtils.isEmpty(account)){
            showToast(getString(R.string.input_account));
            return;
        }
        HashMap<String, String> mParams = new HashMap<>();
        mParams.put("username", account);
        Api.config(ApiConfig.GET_CODE_URL,mParams).getRequest(this,new Callback() {
            @Override
            public void onSuccess(String res) {
                Log.d("res:",res);
                //发送完验证码，直接由服务端验证
                mHandler.sendEmptyMessage(1);
                showToastSync(getString(R.string.publish_code));
            }
            @Override
            public void onFailure(Exception e) {
                showToastSync(getString(R.string.check_network));
            }
        });
    }
}